package com.meltingslime.asciifight.server;

import com.meltingslime.asciifight.server.network.ConnectionListener;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import java.util.Scanner;

/**
 * @author Exit93
 * @version 1.0
 */
public class ASCIIFightServer {

    public static ServerConfig Config;

    private ConnectionListener listener;
    private boolean running = false;
    private static ASCIIFightServer server;

    private Logger log = Logger.getLogger("asciifight.server");

    public ASCIIFightServer(){
        log.info("ASCII Fight Server starting");
        listener = new ConnectionListener(Config.getBindAddress(), Config.getPort());
        start();
        server = this;
        running = true;
        listenCommands();
    }

    public void start(){
        listener.startListening();
    }

    public void stop(){
        listener.stopListening();
        log.info("Stopping server");
    }

    private void listenCommands(){
        Scanner scn = new Scanner(System.in);
        while (running){
            if (scn.nextLine().equals("quit")){
                stop();
                return;
            }
        }
    }

    public static ASCIIFightServer getInstance(){
        return server;
    }

    public static void main (String[] args){
        System.out.println("ASCII Fight Server\n");

        // Load Config
        Config = ServerConfig.create(ServerConfig.CONFIG_PATH);

        // Configure Logger
        PropertyConfigurator.configure(ServerConfig.CONFIG_PATH);

        new ASCIIFightServer();

    }
}
